﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class Goods
    {
        public int Add(Chain.Model.Goods model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Goods(");
            builder.Append("GoodsCode,GoodsClassID,Name,NameCode,Unit,GoodsNumber,SalePercet,GoodsSaleNumber,Price,CommissionType,CommissionNumber,Point,MinPercent,GoodsType,GoodsBidPrice,GoodsRemark,GoodsPicture,GoodsCreateTime,CreateShopID)");
            builder.Append(" values (");
            builder.Append("@GoodsCode,@GoodsClassID,@Name,@NameCode,@Unit,@GoodsNumber,@SalePercet,@GoodsSaleNumber,@Price,@CommissionType,@CommissionNumber,@Point,@MinPercent,@GoodsType,@GoodsBidPrice,@GoodsRemark,@GoodsPicture,@GoodsCreateTime,@CreateShopID)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@GoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@GoodsClassID", SqlDbType.Int, 4), new SqlParameter("@Name", SqlDbType.VarChar, 100), new SqlParameter("@NameCode", SqlDbType.VarChar, 20), new SqlParameter("@Unit", SqlDbType.NVarChar, 10), new SqlParameter("@GoodsNumber", SqlDbType.Int, 4), new SqlParameter("@SalePercet", SqlDbType.Real, 4), new SqlParameter("@GoodsSaleNumber", SqlDbType.Int, 4), new SqlParameter("@Price", SqlDbType.Money, 8), new SqlParameter("@CommissionType", SqlDbType.Int, 4), new SqlParameter("@CommissionNumber", SqlDbType.Float, 8), new SqlParameter("@Point", SqlDbType.Int, 4), new SqlParameter("@MinPercent", SqlDbType.Real, 4), new SqlParameter("@GoodsType", SqlDbType.TinyInt, 1), new SqlParameter("@GoodsBidPrice", SqlDbType.Money, 8), new SqlParameter("@GoodsRemark", SqlDbType.VarChar, 100), 
                new SqlParameter("@GoodsPicture", SqlDbType.VarChar, 50), new SqlParameter("@GoodsCreateTime", SqlDbType.DateTime), new SqlParameter("@CreateShopID", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.GoodsCode;
            cmdParms[1].Value = model.GoodsClassID;
            cmdParms[2].Value = model.Name;
            cmdParms[3].Value = model.NameCode;
            cmdParms[4].Value = model.Unit;
            cmdParms[5].Value = model.GoodsNumber;
            cmdParms[6].Value = model.SalePercet;
            cmdParms[7].Value = model.GoodsSaleNumber;
            cmdParms[8].Value = model.Price;
            cmdParms[9].Value = model.CommissionType;
            cmdParms[10].Value = model.CommissionNumber;
            cmdParms[11].Value = model.Point;
            cmdParms[12].Value = model.MinPercent;
            cmdParms[13].Value = model.GoodsType;
            cmdParms[14].Value = model.GoodsBidPrice;
            cmdParms[15].Value = model.GoodsRemark;
            cmdParms[0x10].Value = model.GoodsPicture;
            cmdParms[0x11].Value = model.GoodsCreateTime;
            cmdParms[0x12].Value = model.CreateShopID;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int AddCustomField(string strGoodsCode, Hashtable customhash)
        {
            DataSet list = new Chain.IDAL.MemCustomField().GetList(" CustomType=2");
            StringBuilder builder = new StringBuilder();
            if (list.Tables[0].Rows.Count > 0)
            {
                builder.Append(" update Goods set ");
                int num = 1;
                string str = "";
                foreach (DataRow row in list.Tables[0].Rows)
                {
                    if (customhash[row["CustomField"].ToString()] != null)
                    {
                        str = customhash[row["CustomField"].ToString()].ToString();
                    }
                    builder.AppendFormat(" {0}='{1}' ", row["CustomField"].ToString(), str);
                    if (num != list.Tables[0].Rows.Count)
                    {
                        builder.Append(",");
                    }
                    num++;
                }
                builder.AppendFormat(" where GoodsCode='{0}'", strGoodsCode);
            }
            return DbHelperSQL.ExecuteSql(builder.ToString());
        }

        public bool Delete(int GoodsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Goods ");
            builder.Append(" where GoodsID=@GoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GoodsID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string GoodsIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Goods ");
            builder.Append(" where GoodsID in (" + GoodsIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool ExeclDataInput(ArrayList sqlArray)
        {
            return DbHelperSQL.ExecuteSqlTran(sqlArray);
        }

        public bool Exists(int GoodsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Goods");
            builder.Append(" where GoodsID=@GoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GoodsID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(string strGoodsCode)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Goods");
            builder.Append(" where GoodsCode=@GoodsCode");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsCode", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = strGoodsCode;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int goodsID, string goodsCode)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select count(1) from Goods");
            builder.Append(" where GoodsCode=@GoodsCode and GoodsID<>@GoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@GoodsID", SqlDbType.Int) };
            cmdParms[0].Value = goodsCode;
            cmdParms[1].Value = goodsID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(string strGoodsCode, int ShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Goods");
            builder.Append(" where GoodsCode=@GoodsCode and ShopID=@ShopID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@ShopID", SqlDbType.Int) };
            cmdParms[0].Value = strGoodsCode;
            cmdParms[1].Value = ShopID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int goodsID, string goodsCode, int ShopID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select count(1) from Goods");
            builder.Append(" where GoodsCode=@GoodsCode and GoodsID<>@GoodsID and ShopID<>@ShopID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@GoodsID", SqlDbType.Int), new SqlParameter("@ShopID", SqlDbType.Int) };
            cmdParms[0].Value = goodsCode;
            cmdParms[1].Value = goodsID;
            cmdParms[2].Value = ShopID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetGoodLists(int PageSize, int PageIndex, out int resCount, string strTime, params string[] strWhere)
        {
            string[] strArray;
            string tableName = "Goods ,GoodsClass";
            //strArray = new string[] { strArray[0].Replace("and 1=1", strTime) };
            strArray = new string[] { ("and 1=1") };
            strWhere[0] = strWhere[0].Substring(0x20);
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, strArray, condition, "MemCreateTime", "MemID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetGoodsExpense(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = this.GetListGoodsSql(column, condition, "totalNumber", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetGoodsID(string GoodsAccount)
        {
            return Convert.ToInt32(DbHelperSQL.Query(string.Format("select GoodsID from dbo.Goods where GoodsCode='{0}'", GoodsAccount)).Tables[0].Rows[0][0]);
        }

        public DataSet GetGoodsInfo(string goodscode, int levelid)
        {
            return DbHelperSQL.Query("select goods.GoodsType,goods.GoodsID,goods.[Name],goods.Price, GoodsClassDiscount.* from goods inner join GoodsClassDiscount on goods.GoodsClassID = GoodsClassDiscount.GoodsClassID " + string.Format(" WHERE goods.goodscode = '{0}' and GoodsClassDiscount.MemLevelID = {1}", goodscode, levelid));
        }

        public DataSet GetGoodsList(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "V_GoodsNumber";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ShopID", "GoodsID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetGoodsListByMember(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "V_GoodsNumberByMember";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "Number", "GoodsID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetGoodsListByNoMember(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "V_GoodsNumberNoMember";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "Number", "GoodsID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetGoodsListByShopID(int PageSize, int PageIndex, int ShopID, out int resCount, params string[] strWhere)
        {
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = this.GetGoodsNumberSql(column, condition, "id", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetGoodsNumberSql(string[] column, string[] condition, string IndexColumn, bool IsAsc, int PageSize, int Page, int RecordCount)
        {
            string str = "";
            string inputSql = "";
            for (int i = 0; i < column.Length; i++)
            {
                str = str + column[i];
                if (i < (column.Length - 1))
                {
                    str = str + ",";
                }
            }
            if ((condition != null) && (condition.Length > 0))
            {
                for (int j = 0; j < condition.Length; j++)
                {
                    if (!(condition[j] == ""))
                    {
                        inputSql = inputSql + condition[j];
                        if (j < (condition.Length - 1))
                        {
                            inputSql = inputSql + " and ";
                        }
                    }
                }
            }
            inputSql = DbHelperSQL.ProcessSql(inputSql);
            SqlParameter[] parameter = new SqlParameter[] { new SqlParameter("@ReturnFields", SqlDbType.VarChar, 500), new SqlParameter("@OrderFields", SqlDbType.VarChar, 0xff), new SqlParameter("@PageSize", SqlDbType.Int, 4), new SqlParameter("@PageIndex", SqlDbType.Int, 4), new SqlParameter("@PKField", SqlDbType.VarChar, 0xff), new SqlParameter("@IsDesc", SqlDbType.Bit), new SqlParameter("@Where", SqlDbType.VarChar, 0x3e8) };
            parameter[0].Value = str;
            parameter[1].Value = IndexColumn;
            parameter[2].Value = PageSize;
            parameter[3].Value = Page;
            parameter[4].Value = IndexColumn;
            parameter[5].Value = IsAsc ? 0 : 1;
            parameter[6].Value = inputSql;
            return DbHelperSQL.ExecuteProcedure("CP_GoodsNumberPage", parameter);
        }

        public DataSet GetGoodsStockList(int ShopId, int MemLevelId, string GoodsIdList, int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "GoodsNumber,SysShop,Goods,GoodsClassDiscount";
            string[] column = new string[] { "GoodsNumber.Number, SysShop.ShopName, GoodsNumber.ShopID, Goods.*, GoodsClassDiscount.MemLevelID, GoodsClassDiscount.ClassDiscountPercent, GoodsClassDiscount.ClassPointPercent" };
            List<string> list = new List<string>();
            for (int i = 0; i < strWhere.Length; i++)
            {
                list.Add(strWhere[i]);
            }
            list.Add(" GoodsNumber.GoodsID = Goods.GoodsID and Goods.GoodsClassID = GoodsClassDiscount.GoodsClassID and GoodsNumber.ShopID = SysShop.ShopID and GoodsNumber.ShopID = GoodsClassDiscount.DiscountShopID");
            if (ShopId != -1)
            {
                list.Add(" GoodsNumber.ShopID = " + ShopId);
            }
            if (MemLevelId == -1)
            {
                list.Add(" GoodsClassDiscount.MemLevelId = (select MIN(MemLevelID) from GoodsClassDiscount)");
            }
            else
            {
                list.Add(" GoodsClassDiscount.MemLevelId = " + MemLevelId);
            }
            if (!string.IsNullOrEmpty(GoodsIdList))
            {
                list.Add(" GoodsNumber.GoodsID in (" + GoodsIdList.Trim() + ")");
            }
            string[] condition = list.ToArray();
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ID", "GoodsNumber.GoodsID", true, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetItemAll(int intGoodsID)
        {
            return DbHelperSQL.Query(" select * from Goods where GoodsID =" + intGoodsID);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select GoodsID,GoodsCode,GoodsClassID,Name,NameCode,Unit,GoodsNumber,SalePercet,GoodsSaleNumber,Price,CommissionType,CommissionNumber,Point,MinPercent,GoodsType,GoodsBidPrice,GoodsRemark,GoodsPicture,GoodsCreateTime,Goods.CreateShopID,ClassName ");
            builder.Append(" FROM Goods,GoodsClass where  Goods.GoodsClassID = GoodsClass.ClassID");
            if (strWhere.Trim() != "")
            {
                builder.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" GoodsID,GoodsCode,GoodsClassID,Name,NameCode,Unit,GoodsNumber,SalePercet,GoodsSaleNumber,Price,CommissionType,CommissionNumber,Point,MinPercent,GoodsType,GoodsBidPrice,GoodsRemark,GoodsPicture,GoodsCreateTime,CreateShopID ");
            builder.Append(" FROM Goods ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int PageSiza, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "Goods";
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "GoodsID", false, PageSiza, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.GoodsID desc");
            }
            builder.Append(")AS Row, T.*  from Goods T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListGoodsSql(string[] column, string[] condition, string IndexColumn, bool IsAsc, int PageSize, int Page, int RecordCount)
        {
            string str = "";
            string inputSql = "";
            for (int i = 0; i < column.Length; i++)
            {
                str = str + column[i];
                if (i < (column.Length - 1))
                {
                    str = str + ",";
                }
            }
            if ((condition != null) && (condition.Length > 0))
            {
                for (int j = 0; j < condition.Length; j++)
                {
                    if (!(condition[j] == ""))
                    {
                        inputSql = inputSql + condition[j];
                        if (j < (condition.Length - 1))
                        {
                            inputSql = inputSql + " and ";
                        }
                    }
                }
            }
            inputSql = DbHelperSQL.ProcessSql(inputSql);
            SqlParameter[] parameter = new SqlParameter[] { new SqlParameter("@ReturnFields", SqlDbType.VarChar, 500), new SqlParameter("@OrderFields", SqlDbType.VarChar, 500), new SqlParameter("@PageSize", SqlDbType.Int, 4), new SqlParameter("@PageIndex", SqlDbType.Int, 4), new SqlParameter("@PKField", SqlDbType.VarChar, 0xff), new SqlParameter("@IsDesc", SqlDbType.Bit), new SqlParameter("@Where", SqlDbType.VarChar, 0xfa0) };
            parameter[0].Value = str;
            parameter[1].Value = IndexColumn;
            parameter[2].Value = PageSize;
            parameter[3].Value = Page;
            parameter[4].Value = IndexColumn;
            parameter[5].Value = IsAsc ? 0 : 1;
            parameter[6].Value = inputSql;
            return DbHelperSQL.ExecuteProcedure("CP_GoodsExpendPage", parameter);
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "Goods,GoodsClass,GoodsNumber";
            string[] column = new string[] { "Goods.*,ClassName,ShopID" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "GoodsCreateTime", "Goods.GoodsID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("GoodsID", "Goods");
        }

        public Chain.Model.Goods GetModel(int GoodsID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 GoodsID,GoodsCode,GoodsClassID,Name,NameCode,Unit,GoodsNumber,SalePercet,GoodsSaleNumber,Price,CommissionType,CommissionNumber,Point,MinPercent,GoodsType,GoodsBidPrice,GoodsRemark,GoodsPicture,GoodsCreateTime,CreateShopID from Goods ");
            builder.Append(" where GoodsID=@GoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsID", SqlDbType.Int, 4) };
            cmdParms[0].Value = GoodsID;
            Chain.Model.Goods goods = new Chain.Model.Goods();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["GoodsID"] != null) && (set.Tables[0].Rows[0]["GoodsID"].ToString() != ""))
            {
                goods.GoodsID = int.Parse(set.Tables[0].Rows[0]["GoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsCode"] != null) && (set.Tables[0].Rows[0]["GoodsCode"].ToString() != ""))
            {
                goods.GoodsCode = set.Tables[0].Rows[0]["GoodsCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GoodsClassID"] != null) && (set.Tables[0].Rows[0]["GoodsClassID"].ToString() != ""))
            {
                goods.GoodsClassID = int.Parse(set.Tables[0].Rows[0]["GoodsClassID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Name"] != null) && (set.Tables[0].Rows[0]["Name"].ToString() != ""))
            {
                goods.Name = set.Tables[0].Rows[0]["Name"].ToString();
            }
            if ((set.Tables[0].Rows[0]["NameCode"] != null) && (set.Tables[0].Rows[0]["NameCode"].ToString() != ""))
            {
                goods.NameCode = set.Tables[0].Rows[0]["NameCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["Unit"] != null) && (set.Tables[0].Rows[0]["Unit"].ToString() != ""))
            {
                goods.Unit = set.Tables[0].Rows[0]["Unit"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GoodsNumber"] != null) && (set.Tables[0].Rows[0]["GoodsNumber"].ToString() != ""))
            {
                goods.GoodsNumber = int.Parse(set.Tables[0].Rows[0]["GoodsNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SalePercet"] != null) && (set.Tables[0].Rows[0]["SalePercet"].ToString() != ""))
            {
                goods.SalePercet = decimal.Parse(set.Tables[0].Rows[0]["SalePercet"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsSaleNumber"] != null) && (set.Tables[0].Rows[0]["GoodsSaleNumber"].ToString() != ""))
            {
                goods.GoodsSaleNumber = int.Parse(set.Tables[0].Rows[0]["GoodsSaleNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Price"] != null) && (set.Tables[0].Rows[0]["Price"].ToString() != ""))
            {
                goods.Price = decimal.Parse(set.Tables[0].Rows[0]["Price"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CommissionType"] != null) && (set.Tables[0].Rows[0]["CommissionType"].ToString() != ""))
            {
                goods.CommissionType = int.Parse(set.Tables[0].Rows[0]["CommissionType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CommissionNumber"] != null) && (set.Tables[0].Rows[0]["CommissionNumber"].ToString() != ""))
            {
                goods.CommissionNumber = decimal.Parse(set.Tables[0].Rows[0]["CommissionNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Point"] != null) && (set.Tables[0].Rows[0]["Point"].ToString() != ""))
            {
                goods.Point = int.Parse(set.Tables[0].Rows[0]["Point"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MinPercent"] != null) && (set.Tables[0].Rows[0]["MinPercent"].ToString() != ""))
            {
                goods.MinPercent = decimal.Parse(set.Tables[0].Rows[0]["MinPercent"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsType"] != null) && (set.Tables[0].Rows[0]["GoodsType"].ToString() != ""))
            {
                goods.GoodsType = int.Parse(set.Tables[0].Rows[0]["GoodsType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsBidPrice"] != null) && (set.Tables[0].Rows[0]["GoodsBidPrice"].ToString() != ""))
            {
                goods.GoodsBidPrice = decimal.Parse(set.Tables[0].Rows[0]["GoodsBidPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsRemark"] != null) && (set.Tables[0].Rows[0]["GoodsRemark"].ToString() != ""))
            {
                goods.GoodsRemark = set.Tables[0].Rows[0]["GoodsRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GoodsPicture"] != null) && (set.Tables[0].Rows[0]["GoodsPicture"].ToString() != ""))
            {
                goods.GoodsPicture = set.Tables[0].Rows[0]["GoodsPicture"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GoodsCreateTime"] != null) && (set.Tables[0].Rows[0]["GoodsCreateTime"].ToString() != ""))
            {
                goods.GoodsCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["GoodsCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CreateShopID"] != null) && (set.Tables[0].Rows[0]["CreateShopID"].ToString() != ""))
            {
                goods.CreateShopID = int.Parse(set.Tables[0].Rows[0]["CreateShopID"].ToString());
            }
            return goods;
        }

        public Chain.Model.Goods GetModel(string GoodsCode)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 GoodsID,GoodsCode,GoodsClassID,Name,NameCode,Unit,GoodsNumber,SalePercet,GoodsSaleNumber,Price,CommissionType,CommissionNumber,Point,MinPercent,GoodsType,GoodsBidPrice,GoodsRemark,GoodsPicture,GoodsCreateTime,CreateShopID from Goods ");
            builder.Append(" where GoodsCode=@GoodsCode");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@GoodsCode", SqlDbType.VarChar, 0xff) };
            cmdParms[0].Value = GoodsCode;
            Chain.Model.Goods goods = new Chain.Model.Goods();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["GoodsID"] != null) && (set.Tables[0].Rows[0]["GoodsID"].ToString() != ""))
            {
                goods.GoodsID = int.Parse(set.Tables[0].Rows[0]["GoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsCode"] != null) && (set.Tables[0].Rows[0]["GoodsCode"].ToString() != ""))
            {
                goods.GoodsCode = set.Tables[0].Rows[0]["GoodsCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GoodsClassID"] != null) && (set.Tables[0].Rows[0]["GoodsClassID"].ToString() != ""))
            {
                goods.GoodsClassID = int.Parse(set.Tables[0].Rows[0]["GoodsClassID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Name"] != null) && (set.Tables[0].Rows[0]["Name"].ToString() != ""))
            {
                goods.Name = set.Tables[0].Rows[0]["Name"].ToString();
            }
            if ((set.Tables[0].Rows[0]["NameCode"] != null) && (set.Tables[0].Rows[0]["NameCode"].ToString() != ""))
            {
                goods.NameCode = set.Tables[0].Rows[0]["NameCode"].ToString();
            }
            if ((set.Tables[0].Rows[0]["Unit"] != null) && (set.Tables[0].Rows[0]["Unit"].ToString() != ""))
            {
                goods.Unit = set.Tables[0].Rows[0]["Unit"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GoodsNumber"] != null) && (set.Tables[0].Rows[0]["GoodsNumber"].ToString() != ""))
            {
                goods.GoodsNumber = int.Parse(set.Tables[0].Rows[0]["GoodsNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SalePercet"] != null) && (set.Tables[0].Rows[0]["SalePercet"].ToString() != ""))
            {
                goods.SalePercet = decimal.Parse(set.Tables[0].Rows[0]["SalePercet"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsSaleNumber"] != null) && (set.Tables[0].Rows[0]["GoodsSaleNumber"].ToString() != ""))
            {
                goods.GoodsSaleNumber = int.Parse(set.Tables[0].Rows[0]["GoodsSaleNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Price"] != null) && (set.Tables[0].Rows[0]["Price"].ToString() != ""))
            {
                goods.Price = decimal.Parse(set.Tables[0].Rows[0]["Price"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CommissionType"] != null) && (set.Tables[0].Rows[0]["CommissionType"].ToString() != ""))
            {
                goods.CommissionType = int.Parse(set.Tables[0].Rows[0]["CommissionType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CommissionNumber"] != null) && (set.Tables[0].Rows[0]["CommissionNumber"].ToString() != ""))
            {
                goods.CommissionNumber = decimal.Parse(set.Tables[0].Rows[0]["CommissionNumber"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Point"] != null) && (set.Tables[0].Rows[0]["Point"].ToString() != ""))
            {
                goods.Point = int.Parse(set.Tables[0].Rows[0]["Point"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MinPercent"] != null) && (set.Tables[0].Rows[0]["MinPercent"].ToString() != ""))
            {
                goods.MinPercent = decimal.Parse(set.Tables[0].Rows[0]["MinPercent"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsType"] != null) && (set.Tables[0].Rows[0]["GoodsType"].ToString() != ""))
            {
                goods.GoodsType = int.Parse(set.Tables[0].Rows[0]["GoodsType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsBidPrice"] != null) && (set.Tables[0].Rows[0]["GoodsBidPrice"].ToString() != ""))
            {
                goods.GoodsBidPrice = decimal.Parse(set.Tables[0].Rows[0]["GoodsBidPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["GoodsRemark"] != null) && (set.Tables[0].Rows[0]["GoodsRemark"].ToString() != ""))
            {
                goods.GoodsRemark = set.Tables[0].Rows[0]["GoodsRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GoodsPicture"] != null) && (set.Tables[0].Rows[0]["GoodsPicture"].ToString() != ""))
            {
                goods.GoodsPicture = set.Tables[0].Rows[0]["GoodsPicture"].ToString();
            }
            if ((set.Tables[0].Rows[0]["GoodsCreateTime"] != null) && (set.Tables[0].Rows[0]["GoodsCreateTime"].ToString() != ""))
            {
                goods.GoodsCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["GoodsCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["CreateShopID"] != null) && (set.Tables[0].Rows[0]["CreateShopID"].ToString() != ""))
            {
                goods.CreateShopID = int.Parse(set.Tables[0].Rows[0]["CreateShopID"].ToString());
            }
            return goods;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM Goods ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetStockRemind(string strWhere)
        {
            return DbHelperSQL.Query(string.Format("select * from  V_GoodsNumber where {0} ", strWhere));
        }

        public DataSet GetStockRemind(string strWhere, int count)
        {
            return DbHelperSQL.Query(string.Format("select top " + count + " * from  V_GoodsNumber where {0} ", strWhere));
        }

        public bool Update(Chain.Model.Goods model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Goods set ");
            builder.Append("GoodsCode=@GoodsCode,");
            builder.Append("GoodsClassID=@GoodsClassID,");
            builder.Append("Name=@Name,");
            builder.Append("NameCode=@NameCode,");
            builder.Append("Unit=@Unit,");
            builder.Append("GoodsNumber=@GoodsNumber,");
            builder.Append("SalePercet=@SalePercet,");
            builder.Append("GoodsSaleNumber=@GoodsSaleNumber,");
            builder.Append("Price=@Price,");
            builder.Append("CommissionType=@CommissionType,");
            builder.Append("CommissionNumber=@CommissionNumber,");
            builder.Append("Point=@Point,");
            builder.Append("MinPercent=@MinPercent,");
            builder.Append("GoodsType=@GoodsType,");
            builder.Append("GoodsBidPrice=@GoodsBidPrice,");
            builder.Append("GoodsRemark=@GoodsRemark,");
            builder.Append("GoodsPicture=@GoodsPicture,");
            builder.Append("GoodsCreateTime=@GoodsCreateTime,");
            builder.Append("CreateShopID=@CreateShopID");
            builder.Append(" where GoodsID=@GoodsID");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@GoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@GoodsClassID", SqlDbType.Int, 4), new SqlParameter("@Name", SqlDbType.VarChar, 100), new SqlParameter("@NameCode", SqlDbType.VarChar, 20), new SqlParameter("@Unit", SqlDbType.NVarChar, 10), new SqlParameter("@GoodsNumber", SqlDbType.Int, 4), new SqlParameter("@SalePercet", SqlDbType.Real, 4), new SqlParameter("@GoodsSaleNumber", SqlDbType.Int, 4), new SqlParameter("@Price", SqlDbType.Money, 8), new SqlParameter("@CommissionType", SqlDbType.Int, 4), new SqlParameter("@CommissionNumber", SqlDbType.Float, 8), new SqlParameter("@Point", SqlDbType.Int, 4), new SqlParameter("@MinPercent", SqlDbType.Real, 4), new SqlParameter("@GoodsType", SqlDbType.TinyInt, 1), new SqlParameter("@GoodsBidPrice", SqlDbType.Money, 8), new SqlParameter("@GoodsRemark", SqlDbType.VarChar, 100), 
                new SqlParameter("@GoodsPicture", SqlDbType.VarChar, 50), new SqlParameter("@GoodsCreateTime", SqlDbType.DateTime), new SqlParameter("@CreateShopID", SqlDbType.Int, 4), new SqlParameter("@GoodsID", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.GoodsCode;
            cmdParms[1].Value = model.GoodsClassID;
            cmdParms[2].Value = model.Name;
            cmdParms[3].Value = model.NameCode;
            cmdParms[4].Value = model.Unit;
            cmdParms[5].Value = model.GoodsNumber;
            cmdParms[6].Value = model.SalePercet;
            cmdParms[7].Value = model.GoodsSaleNumber;
            cmdParms[8].Value = model.Price;
            cmdParms[9].Value = model.CommissionType;
            cmdParms[10].Value = model.CommissionNumber;
            cmdParms[11].Value = model.Point;
            cmdParms[12].Value = model.MinPercent;
            cmdParms[13].Value = model.GoodsType;
            cmdParms[14].Value = model.GoodsBidPrice;
            cmdParms[15].Value = model.GoodsRemark;
            cmdParms[0x10].Value = model.GoodsPicture;
            cmdParms[0x11].Value = model.GoodsCreateTime;
            cmdParms[0x12].Value = model.CreateShopID;
            cmdParms[0x13].Value = model.GoodsID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

